<?php

namespace app\xwam\controller\user;

/**
 * 会员控制器
 *
 * @version 1.0 2016-8-22> sutroon Added.
 */
class Member extends AppbaseController {

    function indexSearch() {
        $search = input('param.');
        $where=[];
        if (!empty($search['word'])) {
            $word = $search['word'];
            $where['_string'] = "user_name like '%$word%' or name like '%$word%' or mobile like '%$word%'";
        }
        if (!empty($search['user_name'])) {
            $where['user_name'] = array('like', '%' . $search['user_name'] . '%');
        }
        return array('search' => $search, 'where' => $where);
    }

    public function index() {
        $asearch = $this->indexSearch();
        $list = db('user_member')->where($asearch['where'])->order('id desc')->paginate();
        $this->assign('search', $asearch['search']);
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return view();
    }

    function getItemJson($id = '', $username = '') {
        $where = array();
        if ($id) {
            $where = array('id' => $id);
        }
        if ($username) {
            $where = array('user_name' => $username);
        }
        if (!$where) {
            return json_encode(null);
        }
        $m_member = new \app\xwam\model\MemberModel();
        return json_encode($m_member->where($where)->find());
    }

    public function exportAction() {
        $m_user = new \app\xwam\model\UserModel();
        $list = $m_user->select();
        if (!$list) {
            $this->error('找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 编辑个人资料
     * @version 1.0 2015-11-11 SoChishun Added.
     */
    function profile_edit() {
        $m_member = new \app\xwam\model\MemberModel();
        $data = $m_member->find_seat($this->loginData['id']);
        $this->assign('data', $data);
        return view();
    }

    public function profile_edit_save() {
        $m_member = new \app\xwam\model\MemberModel();
        $result = $m_member->saveUser($this->loginData['id']);
        $this->dialogClose($result['status'], array('success' => '保存成功!<br /><span style="font-size:12px">如有变更[来电弹屏模式]内容，需刷新浏览器才会生效!</span>', 'error' => $result['info'], 'success_action' => '!window.parent.location.reload();', 'ajax' => 5));
    }

    public function edit($id = 0) {
        $data = array();
        if ($id) {
            $m_member = new \app\xwam\model\MemberModel();
            $data = $m_member->find($id);
        }
        if (!$data) {
            $data['site_id'] = $this->siteId;
        }
        $this->assign('data', $data);
        return view();
    }

    public function editSave() {
        $m_member = new \app\xwam\model\MemberModel();
        $result = $m_member->saveUser($this->loginData['id']);
        $this->dialogJump($result['status'], $result['info']);
    }

    public function delete($id = '') {
        if (!$id) {
            return json_encode(xsfn_message(false, '编号丢失!'));
        }
        $m_member = new \app\xwam\model\MemberModel();
        $result = $m_member->delete_user($id, $this->loginData['id']);
        return json_encode($result);
    }

    public function reset_password($id) {
        if (!$id) {
            return json_encode(xsfn_message(false, '参数有误!'));
        }
        $m_member = new \app\xwam\model\MemberModel();
        $result = $m_member->where(array('id' => $id))->setField('password', '123456');
        return json_encode($result);
    }

}
